/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.tests.db.hibernate.sessions;

import java.util.List;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;

import com.citep.business.sessions.UserSession;
import com.citep.business.User;
import com.citep.db.hibernate.UserDAO;
import com.citep.business.Role;
import com.citep.db.hibernate.RoleDAO;

import junit.framework.TestCase;

public class UserSessionTest extends AbstractTransactionalDataSourceSpringContextTests {

	private static int userId;
	private static int roleId;
	private UserSession session;
	
	public void setAccountSession(UserSession s) {
		session = s;
	}

	protected String[] getConfigLocations() {
		return new String[] { "classpath:applicationContext.xml" };
	}
	
	public void testCreateRoleStringStringBoolean() {
		Role r = session.createRole("test role", "test role description", true);
		roleId = r.getId();
		setComplete();
	}

	public void testCreateUserStringStringStringString() {
		Role r = session.getRole(roleId);
		User u = session.createUser("test firstname", "test lastname", "test password", "test@gmail.com",r);
		assertNotNull(u);
		userId = u.getId();
		setComplete();
	}


	public void testGetUser() {
		User u = session.getUser(userId);
		assertNotNull(u);
		assertEquals(u.getId(),userId);
	}



	public void testUpdateUser() {
		Role r = session.getRole(roleId);

		User u = new UserDAO();
		u.setFirstName("test");
		u.setLastName("test");
		u.setPassword("password");
		u.setEmail("email@yahoo.com");
		u.setRole(r);
		u = session.createUser(u);
		
		assertNotNull(u);
		assertTrue(u.getId() > 0);
	}


	public void testGetUsers() {
		List<User> users = session.getUsers(0,0);
		
		assertTrue(users.size() == 1);
		assertEquals(users.get(0).getId(),userId);
	}

	public void testFindUser() {
		User u = session.findUser("test@gmail.com", "test password");
		assertNotNull(u);
		assertEquals(u.getId(),userId);
	}


	public void testGetRole() {
		Role r = session.getRole(roleId);
		assertNotNull(r);
		assertEquals(r.getId(),roleId);
	}


	public void testCreateRoleRole() {
		
		Role r = new RoleDAO();
		r.setDescription("test");
		r.setName("test");
		r.setEnabled(true);
		r = session.createRole(r);
		
		assertNotNull(r);
		assertTrue(r.getId() > 0);
	}

	public void testUpdateRole() {
		Role r = session.getRole(roleId);
		r.setName("test2");
		
		assertEquals("test2",session.getRole(roleId).getName());
	}


	public void testGetRoles() {
		List<Role> roles = session.getRoles();

		assertNotNull(roles.size());
		assertEquals(roles.size(),1);
		assertEquals(roles.get(0).getId(),roleId);
	}

	public void testFindRole() {
		Role role = session.findRole("test role");
		assertNotNull(role);
		assertEquals(role.getId(),roleId);
	}

	public void testAddRoleToUser() {
		Role role = session.getRole(roleId);
		User user = session.getUser(userId);

		user.setRole(role);
		setComplete();
	}
	
	public void testGetUserRoles() {
		Role role = session.getRole(roleId);
		User user = session.getUser(userId);
		
		assertEquals(user.getRole(),role);
	}
	
	public void testDeleteUserInt() {
		session.deleteUser(userId);
		setComplete();
	}

	public void testGetUserNone() {
		try {
			User u = session.getUser(userId);
			fail("Exception expected");
		} catch (ObjectRetrievalFailureException e) {
			assertEquals(HibernateObjectRetrievalFailureException.class,e.getClass());
		}
	}

	public void testCreateUserUser() {
		Role r = session.getRole(roleId);

		User u = new UserDAO();
		u.setFirstName("test");
		u.setLastName("test");
		u.setEmail("test");
		u.setPassword("password");
		u.setRole(r);
		
		session.createUser(u);
		assertTrue(u.getId() > 0);
		userId = u.getId();
		setComplete();
	}
	
	public void testDeleteUserUser() {
		User u = session.getUser(userId);
		session.deleteUser(u);
		setComplete();
	}

	public void testDeleteRoleInt() {
		session.deleteRole(roleId);
		setComplete();
	}
	
	public void testGetRoleNone() {
		try {
			session.getRole(roleId);
			fail("Exception expected");
		} catch (ObjectRetrievalFailureException e) {
			assertEquals(HibernateObjectRetrievalFailureException.class,e.getClass());
		}

	}

}
